let items=0;
function cau(sum){
    let t=0;
    for(let i=0;i<sum.length;i++){
        if(sum[i].value==1){
            t++;
        }
    }
    return t;
}
function add(){
    let input=document.getElementById("add").value;
    if( input!=""){
        document.getElementsByClassName("items")[0].innerHTML+=`
        <li id="${items}" value="0" onmouseout="no(this)"   onmouseover="yes(this)">
        ${input}       
        <button value="${items}" onclick="out(this)" class="out">删除</button>
        <button  value="${items}" onclick="finish(this)" class="finish">未完成</button>
        </li>
        `;
        items++;
        document.getElementsByClassName("collection")[0].innerHTML=`进度 ${cau(document.getElementsByTagName("li"))} / ${document.getElementsByTagName("li").length}`
        document.getElementById("add").value="";
    }
}
function out(obj){
    if(confirm("你确认删除吗?")==true){
        let parent= document.getElementsByClassName("items")[0];
        let child= document.getElementById(`${obj.value}`);
        parent.removeChild(child);
        document.getElementsByClassName("collection")[0].innerHTML=`进度 ${cau(document.getElementsByTagName("li"))} / ${document.getElementsByTagName("li").length}`
        alert("删除成功")
    }
}
function no(obj){
    $(`#${obj.id} button`).hide();
}
function yes(obj){
    $(`#${obj.id} button`).show();
}
function finish(obj){
    if(obj.innerHTML=="未完成"){
        obj.innerHTML="已完成";
        document.getElementById(`${obj.value}`).style.backgroundColor="#b7e241";
        document.getElementById(`${obj.value}`).value="1";
        document.getElementsByClassName("collection")[0].innerHTML=`进度 ${cau(document.getElementsByTagName("li"))} / ${document.getElementsByTagName("li").length}`
    }
    else{
        obj.innerHTML="未完成";
        document.getElementById(`${obj.value}`).style.backgroundColor="#7adfa8";
        document.getElementById(`${obj.value}`).value="0";
        document.getElementsByClassName("collection")[0].innerHTML=`进度 ${cau(document.getElementsByTagName("li"))} / ${document.getElementsByTagName("li").length}`
    }    
}
$(".all").keydown(function(e){
    if(e.keyCode==13){
        add();
    }
})